<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\logic\activity;


use app\common\model\activity\RechargeActivity;
use app\common\logic\BaseLogic;
use app\common\model\template\Template;
use app\common\service\FileService;
use think\facade\Db;


/**
 * RechargeActivity逻辑
 * Class RechargeActivityLogic
 * @package app\adminapi\logic
 */
class RechargeActivityLogic extends BaseLogic
{


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/02/23 16:43
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            $background = Template::where('id',$params['template_id'])->value('background')??'';
            RechargeActivity::create([
                'admin_id' => $params['admin_id'],
                'platform' => $params['platform'],
                'template_id' => $params['template_id']??0,
                'image' => $params['image'] ? FileService::setFileUrl($params['image']) : '',
                'title' => $params['title'],
                'content' => $params['content'],
                'background' => $background,
                'describe' => $params['describe'],
                'remark' => $params['remark']??'',
                'button' => $params['button'],
                'price' => $params['price'],
                'egold' => $params['egold'],
                'astrict' => $params['astrict'],
                'position' => $params['position'],
                'frequency' => $params['frequency'],
                'scope' => $params['scope'],
                'start_time' => strtotime($params['start_time']),
                'end_time' => strtotime($params['end_time']),
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/02/23 16:43
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            $background = Template::where('id',$params['template_id'])->value('background')??'';
            RechargeActivity::where('id', $params['id'])->update([
                'template_id' => $params['template_id']??0,
                'image' => $params['image'] ? FileService::setFileUrl($params['image']) : '',
                'title' => $params['title'],
                'content' => $params['content'],
                'background' => $background,
                'describe' => $params['describe'],
                'remark' => $params['remark']??'',
                'button' => $params['button'],
                'price' => $params['price'],
                'egold' => $params['egold'],
                'astrict' => $params['astrict'],
                'position' => $params['position'],
                'frequency' => $params['frequency'],
                'scope' => $params['scope'],
                'start_time' => strtotime($params['start_time']),
                'end_time' => strtotime($params['end_time']),
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 删除
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/02/23 16:43
     */
    public static function delete(array $params): bool
    {
        return RechargeActivity::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2024/02/23 16:43
     */
    public static function detail($params): array
    {
        return RechargeActivity::findOrEmpty($params['id'])->toArray();
    }

    /**
     * @notes  更改状态
     * @param array $params
     * @return bool
     * @author heshihu
     * @date 2022/2/21 18:04
     */
    public static function updateStatus(array $params)
    {

        Db::startTrans();
        try {
            $pop = RechargeActivity::where('id',$params['id'])->find();
            if (RechargeActivity::where('status',1)->where('platform',$pop['platform'])->count()>0){
                self::setError('只能同时拥有一个在线活动');
                return false;
            }
            RechargeActivity::update([
                'id' => $params['id'],
                'status' => $params['status']
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
        return true;
    }
}